<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <div class="filter-item">
        <el-input v-model="listQuery.param.name" placeholder="请输入名称" style="width: 200px;" clearable></el-input>
        <el-button type="primary" style="margin-left:10px;" icon="el-icon-search" @click="handleFilter">查询</el-button>
        <el-button v-if="add_cost_type_button_status" style="margin-left:10px;" icon="el-icon-plus" @click="handleAdd">添加</el-button>
      </div>
    </div>

    <el-table :data="costTypeList" style="width: 100%">
      <el-table-column v-if="false" prop="id"></el-table-column>
      <el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
      <el-table-column prop="desc" label="描述" show-overflow-tooltip></el-table-column>
      <el-table-column prop="createUser" label="创建人"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="操作" fixed='right' width="200" v-if="upd_cost_type_button_status || del_cost_type_button_status">
        <template slot-scope="scope">
          <el-button v-if="upd_cost_type_button_status" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button v-if="del_cost_type_button_status" type="danger" icon="el-icon-delete" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.param.page" :page-sizes="[25, 50, 100, 400]" :page-size="listQuery.param.size" layout="total, sizes, prev, pager, next, jumper" :total="costTypeTotal">
    </el-pagination>

    <el-dialog :title="costTypeTextMap[costTypeDialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="45%">
      <el-form :model="costTypeForm" :rules="costTypeFormRules" ref="costTypeForm" label-position="right" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="costTypeForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="costTypeForm.desc" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button @click="handleCancel()">
          取 消
        </el-button>
        <el-button v-if="costTypeDialogStatus=='create'" type="primary" @click="handleCreate()">
          确 定
        </el-button>
        <el-button v-if="costTypeDialogStatus=='update'" type="primary" @click="handleUpdate()">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import {
  addCostType, updCostType, delCostType, getAllCostType
} from '@/api/costTypeInfo';

export default {
  name: 'costType',
  data() {
    return {
      costTypeTextMap: {
        create: "创建",
        update: "修改"
      },
      costTypeDialogStatus: '',
      listQuery: {
        page: 1,
        size: 25,
        param: {
          name: undefined
        }
      },
      costTypeList: [],
      dialogFormVisible: false,
      costTypeTotal: 0,
      costTypeForm: {
        id: 0,
        name: undefined,
        desc: undefined
      },
      costTypeFormRules: {
        name: [
          { required: true, message: "请填写项目单号", trigger: "blur" },
          { min: 0, max: 50, message: '长度在 0 到 50 个字符', trigger: 'blur' }
        ],
        desc: [
          { max: 250, message: '长度在 0 到 255 个字符', trigger: 'blur' }
        ]
      },
      successCode: 20000,

      add_cost_type_button_status: false,
      upd_cost_type_button_status: false,
      del_cost_type_button_status: false
    }
  },
  computed: {
    ...mapGetters(["elements"])
  },
  created() {
    this.handleFilter();
    if (this.elements['add_cost_type']) {
      this.add_cost_type_button_status = true;
    }
    if (this.elements['upd_cost_type']) {
      this.upd_cost_type_button_status = true;
    }
    if (this.elements['del_cost_type']) {
      this.del_cost_type_button_status = true;
    }
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1;
      this.getCostType();
    },
    handleAdd() {
      this.resetFrom();
      this.dialogFormVisible = true;
      this.costTypeDialogStatus = 'create';
    },
    handleCreate() {
      this.$refs["costTypeForm"].validate(valid => {
        if (!valid) {
          return false;
        }
        addCostType(this.costTypeForm).then(resp => {
          if (resp.code === this.successCode) {
            this.handleCancel();
            this.handleFilter();
          }
        });
      })
    },
    handleEdit(row) {
      this.resetFrom();
      this.dialogFormVisible = true;
      this.costTypeDialogStatus = "update";
      this.$nextTick(() => {
        this.costTypeForm.id = row.id;
        this.costTypeForm.name = row.name;
        this.costTypeForm.desc = row.desc;
      });
    },
    handleUpdate() {
      this.$refs["costTypeForm"].validate(valid => {
        if (!valid) {
          return false;
        }
        updCostType(this.costTypeForm).then(resp => {
          if (resp.code === this.successCode) {
            this.handleCancel();
            this.handleFilter();
          }
        });
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getCostType();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getCostType();
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delCostType(id).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.handleFilter();
        });
      });
    },
    handleCancel() {
      this.dialogFormVisible = false;
      this.costTypeDialogStatus = '';
      this.resetFrom();
    },
    getCostType() {
      getAllCostType(this.listQuery).then(resp => {
        if (resp.code === this.successCode) {
          this.costTypeList = resp.datas.datas;
          this.costTypeTotal = resp.datas.total;
        }
      });
    },
    resetFrom() {
      if (this.$refs["costTypeForm"]) {
        this.$refs["costTypeForm"].resetFields();
      }
    }
  }
}
</script>